package model
{
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;

    public class Score extends Sprite
    {
        public static var MAX_COLOR:uint = 0x00FF00;
        public static var MIN_COLOR:uint = 0xFF0000;
        public static var COLOR_NUM:int = 64;
        public static var COLORS:Array = new Array();

        public var score:int;
        public var maxScore:int;

        private var persent:TextField;

        public function Score(x:Number=0, y:Number=0)
        {
            this.x = x;
            this.y = y;
            maxScore = 1000;
            score = 0;
            for(var i:int=1; i<=Score.COLOR_NUM; i++)
            {
                Score.COLORS.push(this.createColor(i, Score.COLOR_NUM));
            }
            persent = new TextField();
            persent.text = "0%";
            persent.textColor = Score.MIN_COLOR;
            persent.x = 2;
            persent.y = 2;
            persent.width = 30;
            persent.height = 50;
            addChild(persent);
        }

        public function display():void
        {
            var l:int = int(Score.COLOR_NUM * Number((score % maxScore) / maxScore));
            persent.textColor = Score.COLORS[l];
            persent.text = int((100 * l) / Score.COLOR_NUM).toString() + "%";
            graphics.clear();
            graphics.lineStyle(1, Score.COLORS[l]);
            graphics.beginFill(0x000000, 0);
            graphics.moveTo(-2, 4);
            graphics.lineTo(32, 4);
            graphics.lineTo(32, -4*(Score.COLOR_NUM-1));
            graphics.lineTo(-2, -4*(Score.COLOR_NUM-1));
            graphics.endFill();
            for(var i:int=0; i<Score.COLOR_NUM-1; i++)
            {
                graphics.lineStyle(1, 0x000000, 0);
                graphics.beginFill(0xFFFFFF, 0.2);
                graphics.moveTo(0, -4*i);
                graphics.lineTo(30, -4*i);
                graphics.lineTo(30, -4*i+2);
                graphics.lineTo(0, -4*i+2);
                graphics.endFill();
            }
            for(i=0; i<l; i++)
            {
                graphics.lineStyle(1, Score.COLORS[i], 0.5);
                graphics.beginFill(Score.COLORS[i], 1);
                graphics.moveTo(0, -4*i);
                graphics.lineTo(30, -4*i);
                graphics.lineTo(30, -4*i+2);
                graphics.lineTo(0, -4*i+2);
                graphics.endFill();
            }
        }

        public function createColor(v:int, m:int):uint
        {
            var p:uint = v % m;
            var r:uint = (Score.MAX_COLOR >> 16) - (Score.MIN_COLOR >> 16);
            var g:uint = ((Score.MAX_COLOR >> 8) & 0xFF) - ((Score.MIN_COLOR >> 8) & 0xFF);
            var b:uint = (Score.MAX_COLOR & 0xFF) - (Score.MIN_COLOR & 0xFF);
            r = (Score.MIN_COLOR >> 16) + ((r * p) / m);
            g = ((Score.MIN_COLOR >> 8) & 0xFF) + ((g * p) / m);
            b = (Score.MIN_COLOR & 0xFF) + ((b * p) / m);
            return (r << 16) + (g << 8) + b;
        }
    }
}
